/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-02
 * Time: 17:26
 */
public class MySingleList implements IList{

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    private ListNode head;
    private int size;

    @Override
    public void addFirst(int data) {
        ListNode cur = new ListNode(data);
        if (this.head == null) {
            this.head = cur;
            this.size++;
            return;
        }
        cur.next = this.head;
        this.head = cur;
        this.size++;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
            this.size++;
            return;
        }
        ListNode cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        this.size++;
    }

    @Override
    public void addIndex(int index, int data) {
        try {
            isIllegal(index);
            //假设是头插
            if (index == 0) {
                addFirst(data);
                return;
            }
            //假设是尾插
            if(index == this.size) {
                addLast(data);
                return;
            }
            //中间插入
            ListNode node = new ListNode(data);
            ListNode cur = findIndex(index);
            node.next = cur.next;
            cur.next = node;
            this.size++;
        }catch (AbnormalSubscript e) {
            e.printStackTrace();
        }
    }
    //判断下标是否合法
    private void isIllegal(int index) {
        if (index < 0 || index > this.size) {
            throw new AbnormalSubscript("下标异常");
        }
    }
    //获取要插入位置的前一个元素
    private ListNode findIndex(int index) {
        ListNode cur = head;
        int t = index - 1;
        while (t > 0) {
            cur = cur.next;
            t--;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode node = this.head;
        while (node != null) {
            if(node.val == key) {
                return true;
            }
            node = node.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //先判断链表为不为空
        if (this.size == 0) {
            System.out.println("链表为空，无法删除");
            return;
        }
        //假设要删的元素在第一位
        if (this.head.val == key) {
            this.head = this.head.next;
            this.size--;
            return;
        }
        //假设不在第一位
        ListNode pre = findKey(key);
        if (pre == null) {
            System.out.println("链表中没有你要删除的元素！");
            return;
        }
        pre.next = pre.next.next;
        this.size--;
    }
    //获取要被删除元素的前一个元素
    private ListNode findKey(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        //先判断链表是否为空
        if (this.size == 0) {
            System.out.println("链表为空，无法删除！");
            return;
        }
        boolean x = false;
        ListNode slow = this.head;
        ListNode fast = this.head.next;
        //假设被删除的元素全在头结点之后
        while (fast != null) {
            if (fast.val == key) {
                slow.next = fast.next;
                fast = fast.next;
                this.size--;
                x = true;
            }else {
                slow = fast;
                fast = fast.next;
            }
        }
        //假设被删除的元素在头结点
        if (this.head.val == key) {
            this.head = this.head.next;
            this.size--;
            x = true;
        }
        if (x == false) {
            System.out.println("链表中没有你要删除的元素！");
        }
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public void clear() {
        this.head = null;
        this.size = 0;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void func() {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);

        this.head = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
    }
}
